package com.atmc.controller;

import com.atmc.entity.ResponseResult;
import com.atmc.entity.User;
import com.atmc.entity.UserInfo;
import com.atmc.service.ArtLikerService;
import com.atmc.service.LoginService;
import com.atmc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    LoginService loginService;

    //登录接口
    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){

        return loginService.login(user);
    }
    //退出登录
    @PostMapping("/user/outLogin")
    public ResponseResult outlogin(){
        return loginService.outLogin();
    }
    //注册账号
    @PostMapping("/user/register")
    public ResponseResult zhuce(@RequestBody User user){
        String password = user.getPassword();
        if (ObjectUtils.isEmpty(user.getUserName())||ObjectUtils.isEmpty(password)){
            return new ResponseResult(400,"注册失败，重新注册");
        }
        String encode = passwordEncoder.encode(password);
        user.setPassword(encode);
        userService.insertOne(user);
        return new ResponseResult(200,"注册成功");
    }

    //修改账号
    @PostMapping("/user/update")
    public ResponseResult updatePassword(@RequestBody User user){
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        User user1 = userService.selectOne(user.getUserName());
        if (ObjectUtils.isEmpty(user1)){
            return new ResponseResult(400,"修改失败");
        }
        userService.updateOne(user);
        return new ResponseResult(200,"修改成功");
    }
    //修改个人信息
    @PostMapping("/updateUserInfo")
    public ResponseResult updateUserInfo(@RequestParam("userName") String userName,
                                         @RequestParam("sex") String sex,
                                         @RequestParam(required = false,value = "file") MultipartFile file
    ){
        return userService.updateUserInfo(userName,sex,file);
    }

}
